package com.itheima.reggie.commen;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

//定义一个全局异常处理类  (处理带有RestController，Controller注解的类发生的异常)
@RestControllerAdvice(annotations = {RestController.class, Controller.class})
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException .class)
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException e){
        System.out.println("发生异常，异常信息"+e.getMessage());
        return R.error("添加失败");
    }

    @ExceptionHandler(CategoryException.class)
    public R<String> categoryException(CategoryException e){
        return R.error(e.getMessage());
    }
}
